package service;

import java.util.List;

import bean.User;
import bean.UserRowMapper;
import common.BaseDAO;

public class UserDAO extends BaseDAO{
	//注册方法
	public void addUserByTel(String tel,String psw) {
		String sql = "insert into user_info(tel,password) values(?,?)";
		this.update(sql, tel,psw);
	}
	public void addUserByMail(String mail,String psw,String tel) {
		String sql = "insert into user_info(mail,password,tel) values(?,?,?)";
		this.update(sql, mail,psw,tel);
	}
	
	//检索是否存在手机号
	public User queryByTel(String tel) {
		String sql = "select * from user_info where tel=?";
		List<User> list = this.query(sql,new UserRowMapper(), tel);
		if(list!=null&&list.size()>0) {
			return list.get(0) ;
		}
		return null;
	}
	//检索是否存在邮箱
	public User queryByMail(String mail) {
		String sql = "select * from user_info where mail=?";
		List<User> list = this.query(sql,new UserRowMapper(), mail);
		if(list!=null&&list.size()>0) {
			return list.get(0) ;
		}
		return null;
	}
	//登录方法
	//通过手机号登录
	public User loginByTel(String tel,String password) {
		String sql = "select * from user_info where tel=? and password=?";
		List<User> list = this.query(sql,new UserRowMapper(), tel,password);
		if(list!=null&&list.size()>0) {
			return list.get(0) ;
		}
		return null;
	}
	//通过邮箱登录
	public User loginByMail(String mail,String password) {
		String sql = "select * from user_info where mail=? and password=?";
		List<User> list = this.query(sql, new UserRowMapper(), mail,password);
		if(list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	//根据userid增加用户的points/exp/和改变level
	public void updatePointsAndExpByUserid(int points,int exp,int level,int userid){
		String sql="update user_info set points=points+?,exp=exp+?,level=? where userid=?";
		this.update(sql, points,exp,level,userid);
	}

}
